﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chbl.DAL;
using Chbl.Models;

namespace Chbl.Controllers.AdminRole
{
    [Authorize(Roles = "admin")]
    public class AdminTeamController : Controller
    {
        private readonly ChblDb _db;

        public AdminTeamController(ChblDb db)
        {
            _db = db;
        }
        //
        // GET: /AdminTeam/

        public ViewResult Index()
        {
            return View(_db.Teams.ToList());
        }

        //
        // GET: /AdminTeam/Details/5

        public ViewResult Details(int id)
        {
            Team team = _db.Teams.Find(id);
            return View(team);
        }

        //
        // GET: /AdminTeam/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AdminTeam/Create

        [HttpPost]
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                _db.Teams.Add(team);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(team);
        }

        //
        //GET: /AdminTeam/AddToSeasonDivision

        public ActionResult AddToSeason(int id)
        {
            ViewBag.SeasonId = new SelectList(_db.Seasons, "SeasonId", "Number");
            ViewBag.DivisionId = new SelectList(_db.Divisions, "DivisionId", "Name");
            Session.Add("TeamId", id);
            return View();
        }

        //
        //SET: /AdminTeam/AddToSeasonDivision

        [HttpPost]
        public ActionResult AddToSeason(SeasonDivision seasonDiv)
        {
            if (ModelState.IsValid)
            {
                var team = _db.Teams.Find(Session["TeamId"]);
                var seasonDivisions =
                    _db.SeasonDivisions
                    .Where(
                        sd => 
                            sd.DivisionId == seasonDiv.DivisionId 
                            && sd.SeasonId == seasonDiv.SeasonId);
                if (seasonDivisions.Count() == 0)
                {
                    seasonDiv.Teams = new List<Team>();
                    seasonDiv.Teams.Add(team);
                    _db.SeasonDivisions.Add(seasonDiv);
                }
                else
                {
                    var existingSD = seasonDivisions.First();
                    existingSD.Teams.Add(team);
                    _db.Entry(existingSD).State = EntityState.Modified;
                }                
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /AdminTeam/Edit/5
 
        public ActionResult Edit(int id)
        {
            Team team = _db.Teams.Find(id);
            return View(team);
        }

        //
        // POST: /AdminTeam/Edit/5

        [HttpPost]
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(team).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(team);
        }

        //
        // GET: /AdminTeam/Delete/5
 
        public ActionResult Delete(int id)
        {
            Team team = _db.Teams.Find(id);
            return View(team);
        }

        //
        // POST: /AdminTeam/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Team team = _db.Teams.Find(id);
            _db.Teams.Remove(team);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}